package com.bp;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.pool.OracleDataSource;

public class DbBean {


    private String url = "jdbc:oracle:thin:@127.0.1:1521:XE";
    private String user = "HR";
    private String password = "110590";
    private Connection koneksi;
    private Statement st;

    public Connection DbBean() throws SQLException {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {

            koneksi = DriverManager.getConnection(url, user, password);
            System.out.println("koneksi berhasil");
            st = koneksi.createStatement();

        } catch (SQLException ex) {
            Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return koneksi;
    }

    public Connection getKoneksi() {
        try {
            OracleDataSource ds;
            ds = new OracleDataSource();

            ds.setURL(url);
            koneksi = (Connection) ds.getConnection(user, password);
            System.out.println("Koneksi berhasil");
            return koneksi;
        } catch (SQLException ex) {
            System.out.println("Koneksi gagal");
            return null;
        }
    }

    public void setKoneksi(Connection koneksi) {
        this.koneksi = koneksi;
    }

    public Statement getSt() {
        return st;
    }

    public void setSt(Statement st) {
        this.st = st;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

//    public int UpdateData(String sql) throws SQLException{
//        int i=0;
//        i=st.executeUpdate(sql);
//        return i; 
//    }
//    public ResultSet lihatData(String sql)throws SQLException{
//        ResultSet rs=null;
//        rs=st.executeQuery(sql);
//        return rs;
//    }
    public void tutupKoneksi() throws SQLException {
        if (koneksi != null) {
            koneksi.close();
        }
    }

    public Connection bukaKoneksi() throws SQLException {
        koneksi = DriverManager.getConnection(url, user, password);
        return koneksi;
    }

    public boolean isConnected() {
        if (koneksi != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getClosed() {
        if (isConnected()) {
            try {
                koneksi.close();
                koneksi = null;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }

        public static void main(String args[]) {
            DbBean conn = new DbBean();
            try {
                
                String sql = "select * from Paket";
                
//                ResultSet rs = conn.getSt().executeQuery(sql);
                Statement statement = conn.getKoneksi().createStatement();
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next()) {
                    String id_Biro =rs.getString("ID_PAKET");
                   System.out.println("id_paket:" + id_Biro);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
//        
//     static String jdbcURL = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
//    static String user = "MHS105314091";
//    static String password = "MHS105314091";
//    static Connection connection;
//
//    
//    
//    public static Connection getKoneksi() {
//        try {
//            OracleDataSource ds;
//            ds = new OracleDataSource();
//
//            ds.setURL(jdbcURL);
//            connection = (Connection) ds.getConnection(user, password);
//            System.out.println("Koneksi berhasil");
//            return connection;
//        } catch (SQLException ex) {
//            System.out.println("Koneksi gagal");
//            return null;
//        }
//    }
//////
////   public static Connection getDBConnection() throws SQLException {
////
////        try {
////            OracleDataSource ds;
////            ds = new OracleDataSource();
////
////            ds.setURL(jdbcURL);
////            connection = (Connection) ds.getConnection(user, password);
////            System.out.println("Koneksi berhasil");
////            return connection;
////        } catch (SQLException ex) {
////            System.out.println("Koneksi gagal");
////            return null;
////        }
////    }
//
//    public boolean isConnected() {
//        if (connection != null) {
//            return true;
//        } else {
//            return false;
//        }
//    }
//
//    public boolean getClosed() {
//        if (isConnected()) {
//            try {
//                connection.close();
//                connection = null;
//            } catch (SQLException ex) {
//            }
//            return true;
//        } else {
//            return false;
//        }
//    }
//
//    public String getJdbcURL() {
//        return jdbcURL;
//    }
//
//    public void setJdbcURL(String jdbcURL) {
//        DbBean.jdbcURL = jdbcURL;
//    }
//
//    public String getUser() {
//        return user;
//    }
//
//    public void setUser(String user) {
//        DbBean.user = user;
//    }
//
//    public String getPassword() {
//        return password;
//    }
//
//    public void setPassword(String password) {
//        DbBean.password = password;
//    }
//
//    public Connection getConnection() {
//        return connection;
//    }
//
//    public void setConn(Connection connection) {
//        DbBean.connection = connection;
//    }
//
//    public static void main(String[] args) throws SQLException {
//        DbBean conn = new DbBean();
//        DbBean.getKoneksi();
//        conn.getClosed();
////   
//    }
    }
